package com.tiantian.app.data.mo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import com.infindo.frame.data.net.CallbackListener;
import com.infindo.frame.util.DateUtil;
import com.infindo.frame.util.ImageUtil;
 

public class XmlMessage implements Comparable<XmlMessage>{
	static SimpleDateFormat FORMATTER = 
	  new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
	private String title;
	private String link;
	private String description;
	private Date date;
    private String imgUrl;
    private String fulWord;//content
	private Drawable image;
    
    private String photoId;
    private String galleryId;
    private String photoTitle;
    private String primary;
    
    public String getPrimary() {
		return primary;
	}

	public void setPrimary(String primary) {
		this.primary = primary;
	}

    public String getPhotoId() {
		return photoId;
	}

	public void setPhotoId(String photoId) {
		this.photoId = photoId;
	}

	public String getGalleryId() {
		return galleryId;
	}

	public void setGalleryId(String galleryId) {
		this.galleryId = galleryId;
	}

	public String getPhotoTitle() {
		return photoTitle;
	}

	public void setPhotoTitle(String photoTitle) {
		this.photoTitle = photoTitle;
	}

    
    
    public String getFulWord() {
    	return fulWord;
    }
    
    public void setFulWord(String fulWord) {
    	this.fulWord = fulWord;
    }

	public String getImgUrl() {
		return imgUrl;
	}

	public void setImgUrl(String imgUrl) {
	    this.imgUrl = imgUrl;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title.trim();
	}
	// getters and setters omitted for brevity 
	public String getLink() {
		return link;
	}
	
	public void setLink(String link) {
	    this.link =  link;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description.trim();
	}

	public String getDate() {
		if(date==null)
			return "";
//		String dateTime = FORMATTER.format(this.date);
//		Log.v("dateTime",dateTime);
		return DateUtil.getUIDateTime(date);
	}

	public void setDate(String date) {
		// pad the date if necessary
		while (!date.endsWith("00")){
			date += "0";
		}
		try {
			this.date = FORMATTER.parse(date.trim());
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
	
	public XmlMessage copy(){
		XmlMessage copy = new XmlMessage();
		copy.imgUrl = imgUrl;
		copy.title = title;
		copy.link = link;
		copy.description = description;
		copy.date = date;
		copy.fulWord = fulWord;
		copy.galleryId = galleryId;
		copy.photoId = photoId;
		copy.photoTitle = photoTitle;
		copy.primary = primary;
		return copy;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("imgByte:");
		sb.append(imgUrl);
		sb.append('\n');
		sb.append("Title: ");
		sb.append(title);
		sb.append('\n');
		sb.append("Date: ");
		sb.append(this.getDate());
		sb.append('\n');
		sb.append("Link: ");
		sb.append(link);
		sb.append('\n');
		sb.append("Description: ");
		sb.append(description);
		sb.append('\n');
		sb.append("fulWord: ");
		sb.append(fulWord);
		sb.append('\n');
		sb.append("galleryId: ");
		sb.append(galleryId);
		sb.append('\n');
		sb.append("photoId: ");
		sb.append(photoId);
		sb.append('\n');
		sb.append("photoTitle: ");
		sb.append(photoTitle);
		sb.append('\n');
		sb.append("primary: ");
		sb.append(primary);
		sb.append('\n');
		return sb.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((link == null) ? 0 : link.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		result = prime * result + ((imgUrl == null) ? 0 : imgUrl.hashCode());
		result = prime * result + ((fulWord == null) ? 0 : fulWord.hashCode());
		result = prime * result + ((galleryId == null) ? 0 : galleryId.hashCode());
		result = prime * result + ((photoId == null) ? 0 : photoId.hashCode());
		result = prime * result + ((photoTitle == null) ? 0 : photoTitle.hashCode());
		result = prime * result + ((primary == null) ? 0 : primary.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		XmlMessage other = (XmlMessage) obj;
		if(imgUrl == null){
			if(other.imgUrl!= null)
				return false;
		}else if(!imgUrl.equals(other.imgUrl))
			return false;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (fulWord == null) {
			if (other.fulWord != null)
				return false;
		} else if (!fulWord.equals(other.fulWord))
			return false;
		if (link == null) {
			if (other.link != null)
				return false;
		} else if (!link.equals(other.link))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		if (galleryId == null) {
			if (other.galleryId != null)
				return false;
		} else if (!galleryId.equals(other.galleryId))
			return false;
		if (photoId == null) {
			if (other.photoId != null)
				return false;
		} else if (!photoId.equals(other.photoId))
			return false;
		if (photoTitle == null) {
			if (other.photoTitle != null)
				return false;
		} else if (!photoTitle.equals(other.photoTitle))
			return false;
		if (primary == null) {
			if (other.primary != null)
				return false;
		} else if (!primary.equals(other.primary))
			return false;
		return true;
	}

	public int compareTo(XmlMessage another) {
		if (another == null) return 1;
		// sort descending, most recent first
		return another.date.compareTo(date);
	}

//	/* (non-Javadoc)
//	 * @see com.infindo.frame.data.net.CallbackListener#callback(java.lang.String, int, java.lang.Object)
//	 */
//Override
//	public synchronized void callback(String type, int position, Object obj) {
//		// TODO Auto-generated method stub
//		if("getImage".equals(type)){
//			if (obj instanceof byte[]) {
////				Log.e("imageCallback", ""+obj);
//				Bitmap tempBitmap = ImageUtil.Byte2Bitmap((byte[]) obj);
//				if (tempBitmap != null) {
//					image = new BitmapDrawable(tempBitmap);
//				}
//			}
//		}
//	}
	
 
	
 

	public synchronized Drawable getImage() {
//		if(image==null){
//			if(bolGetImage)
//				 return null;
//			else{
//				bolGetImage = true;
//				Log.e("imageurl", "**************************"+imgUrl);
//				if(imgUrl!=null)
//					DataProcessor.getInstance().imageProc(imgUrl, "jpeg", "getImage", this, 0);
//			}
//		}
		return image;
	}

	public void setImage(Drawable image) {
		this.image = image;
	}
	
	
}
